package com.jiezhuo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jiezhuo.entity.cmeduSchool.Xxdw;
import com.jiezhuo.entity.cmeduSchool.XxdwXq;
import com.jiezhuo.handler.RzException;
import com.jiezhuo.service.cmeduSchool.XxdwService;
import com.jiezhuo.service.cmeduSchool.XxdwXqService;
import com.jiezhuo.utils.R;
import com.jiezhuo.utils.SecurityUtils;
import com.jiezhuo.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.UUID;

/**
 * 服务控制器
 *
 * @author kancy
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2024-06-06 08:59:27
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@Api(tags = "校区管理")
@RequestMapping("/xxdwXq")
public class XxdwXqController {

    private final XxdwXqService xxdwXqService;

    private final XxdwService xxdwService;

    @ApiOperation("新增分校区")
    @PostMapping("addCampus")
    public R addCampus(@RequestBody XxdwXq xxdwXq) {

        beforeVerify(xxdwXq);

        // 查询 学校信息
        Xxdw prop = new Xxdw();
        prop.setNewXxdm(xxdwXq.getXxdm());

        QueryWrapper<Xxdw> xxdwQueryWrapper = new QueryWrapper<>(prop);
        Xxdw XxdwInfo = xxdwService.getOne(xxdwQueryWrapper);

        // 增加新校区信息
        xxdwXq.setXqid(UUID.randomUUID().toString());
        xxdwXq.setXxid(XxdwInfo.getXxid());

        xxdwXq.setOperateTime(new Date());
        xxdwXq.setOperator(SecurityUtils.getCurrentUserId());

        xxdwXq.setNewXqdm(xxdwXq.getXqdm());
        xxdwXq.setNewXxdm(xxdwXq.getXxdm());

        boolean save = xxdwXqService.save(xxdwXq);
        return R.ok().data("start", save);
    }

    @ApiOperation("修改分校区")
    @PostMapping("updateCampus")
    public R updateCampus(@RequestBody XxdwXq xxdwXq) {

        updateBeforeVerify(xxdwXq);
        xxdwXq.setOperator(SecurityUtils.getCurrentUserId());
        xxdwXq.setOperateTime(new Date());

        UpdateWrapper<XxdwXq> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("NEW_XQDM", xxdwXq.getXqdm());

        boolean update = xxdwXqService.update(xxdwXq, updateWrapper);

        return R.ok().data("start", update);
    }

    @ApiOperation("获取所有校区")
    @PostMapping("getAllXq")
    public R getAllXq() {
        return R.ok().data("start", xxdwXqService.list());
    }


    private void beforeVerify(XxdwXq xxdwXq) {

        if (StringUtils.isStringNotExist((xxdwXq.getXqdm()))) {
            throw new RzException(500, "校区代码必须填写");
        }

        if (StringUtils.isStringNotExist((xxdwXq.getXqmc()))) {
            throw new RzException(500, "校区全称必须填写");
        }

        if (StringUtils.isStringNotExist((xxdwXq.getXqjc()))) {
            throw new RzException(500, "校区简称必须填写");
        }
    }


    private void updateBeforeVerify(XxdwXq xxdwXq) {
        if (StringUtils.isStringNotExist((xxdwXq.getXqmc()))) {
            throw new RzException(500, "校区全称必须填写");
        }

        if (StringUtils.isStringNotExist((xxdwXq.getXqjc()))) {
            throw new RzException(500, "校区简称必须填写");
        }
    }


}